package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hngm.bean.Product;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.ProductDao;

/**
 * Servlet implementation class ProductServlet
 */
public class ProductServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		//创建ProductDao
		ProductDao productDao = new ProductDao();
		//获取请求中的参数url
		String url = request.getParameter("url");
		 if("queryProductListByParam".equals(url)){
			//获取前端传过来的查询条件
			String productname = request.getParameter("productname");
		    String beginprice = request.getParameter("beginprice");
		    String endprice = request.getParameter("endprice");
		    String del = request.getParameter("del");
		    String orderby = request.getParameter("orderby");
			
			//获取LayUI穿过来的分页信息
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = Integer.parseInt(request.getParameter("page"));
			
			//当前页开始的索引
			int beginIndex = (page - 1) * limit;
			
			//根据传过来得查询条件去查询产品列表
			ArrayList<Product> pList = productDao.queryProductListByParm(productname,beginprice,endprice,del,orderby,beginIndex,limit);
			
			//查询数据库中的数量
			int count = productDao.queyrProductListByParamCount(productname,beginprice,endprice,del,orderby);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, pList);
			out.print(str);
		}else if("add".equals(url)){
			//添加产品逻辑
			//1、获取用户有输入的产品信息
			String name = request.getParameter("name");
			String materail = request.getParameter("materail");
			String saleprice = request.getParameter("saleprice");
			String marketprice = request.getParameter("marketprice");
			String brand = request.getParameter("brand");
			String model = request.getParameter("model");
			String style = request.getParameter("style");
			String color = request.getParameter("color");
			String stylepositioning = request.getParameter("stylepositioning");
			String elements = request.getParameter("elements");
			long producttypetable_id = Long.parseLong(request.getParameter("producttypetable_id"));
			String mainpic = request.getParameter("mainpic");
			int pack = Integer.parseInt(request.getParameter("pack"));
			int customized = Integer.parseInt(request.getParameter("customized"));
			int recommend = Integer.parseInt(request.getParameter("recommend"));
			int rotation = Integer.parseInt(request.getParameter("rotation"));
			String rotationpic = request.getParameter("rotationpic");
			Product p = new Product(name, materail, saleprice, marketprice, brand, model, style, pack, color, stylepositioning, customized, elements, producttypetable_id, mainpic, rotationpic, rotation, recommend);
			
			//2、将产品信息插入到表中（调用DAO）
			int result = productDao.insertProduct(p);
			//3、返回结果前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		}else if("del".equals(url)){
			//删除产品逻辑
			//1、获取前端传过来的产品Id
			String productId = request.getParameter("productId");
			//2、根据产品Id删除产品信息
			int result = productDao.delProduct(productId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		}else if("edit".equals(url)){
			//实现编辑的功能
			//编辑产品逻辑
			//1、更新用户有输入的产品信息
			long id = Long.parseLong(request.getParameter("id"));
			String name = request.getParameter("name");
			String materail = request.getParameter("materail");
			String saleprice = request.getParameter("saleprice");
			String marketprice = request.getParameter("marketprice");
			String brand = request.getParameter("brand");
			String model = request.getParameter("model");
			String style = request.getParameter("style");
			String color = request.getParameter("color");
			String stylepositioning = request.getParameter("stylepositioning");
			String elements = request.getParameter("elements");
			long producttypetable_id = Long.parseLong(request.getParameter("producttypetable_id"));
			String mainpic = request.getParameter("mainpic");
			int pack = Integer.parseInt(request.getParameter("pack"));
			int customized = Integer.parseInt(request.getParameter("customized"));
			int recommend = Integer.parseInt(request.getParameter("recommend"));
			int rotation = Integer.parseInt(request.getParameter("rotation"));
			String rotationpic = request.getParameter("rotationpic");
			Product p = new Product(name, materail, saleprice, marketprice, brand, model, style, pack, color, stylepositioning, customized, elements, producttypetable_id, mainpic, rotationpic, rotation, recommend);
			p.setId(id);
			//2、将产品信息插入到表中（调用DAO）
			int result = productDao.editProduct(p);
			//3、返回结果前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		
		}
		out.flush();
		out.close();
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
